<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" label="提交时间">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" label="意向客户姓名或单位名称">
        <template slot-scope="scope">
          <span>{{ scope.row.userTip }}</span>
        </template>
      </el-table-column>

      <el-table-column width="240px" align="center" label="手机号码">
        <template slot-scope="scope">
          <span>{{ scope.row.mobile }}</span>
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="状态" width="110">
        <template slot-scope="{row}">
          <el-tag :type="row.status | statusFilter">
            {{ row.status | statusFilterText }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="120">
        <template slot-scope="scope">
          <!--          <router-link :to="'/example/edit/'+scope.row.id">-->

          <el-button v-if="scope.row.status==0" type="primary" size="small" icon="el-icon-edit" @click="updateStatus(scope.row.id)">
            已处理
          </el-button>
          <!--          </router-link>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { fetchList, updateStatus } from '@/api/intended/intend-customer'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'IntendedCustomerList',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'info'
      }
      return statusMap[status]
    },
    statusFilterText(status) {
      const statusMap = {
        1: '已回访',
        0: '待回访'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    updateStatus(id) {
      updateStatus(id).then(response => {
        // eslint-disable-next-line eqeqeq
        if (response.code == 20000) {
          this.getList()
        }
      })
    },
    getList() {
      this.listLoading = true
      this.listQuery.pageNum = this.listQuery.page
      console.log(JSON.stringify(this.listQuery))
      fetchList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
